package com.yang.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.yang.config.AlipayConfig;
import com.yang.utils.info.AlipayBean;
import org.springframework.boot.autoconfigure.batch.BatchDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yangdacheng
 * @title: AliPayUtils 支付宝支付
 * @projectName note_api
 * @description: TODO
 * @date 2022/3/9 11:46 上午
 */
@Component
public class AliPayUtils {

    @Resource
    private AlipayConfig alipayConfig;
    @Resource
    private AlipayBean alipayBean;

    public String payInPC(String orderID,String payMoney,String orderName,String orderDesc) {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = alipayConfig.alipayClient();
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(alipayBean.getReturnUrl());
        alipayRequest.setNotifyUrl(alipayRequest.getNotifyUrl());
        String result = null;
        try {
            //商户订单号，商户网站订单系统中唯一订单号，必填
            String out_trade_no = orderID;
            //付款金额，必填
            String total_amount = payMoney;
            //订单名称，必填
            String subject = orderName;
            //商品描述，可空
            String body = orderDesc;

            alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                    + "\"total_amount\":\""+ total_amount +"\","
                    + "\"subject\":\""+ subject +"\","
                    + "\"body\":\""+ body +"\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
            //请求
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        //输出
        return result;
    }
}
